# -*- coding: utf-8 -*-
import matplotlib
matplotlib.use('Agg')
import pylab as pl

import pyecharts

def make_graph(cpu,mem,path,logger):
    tmp_cpu = []
    tmp_mem = []
    tmp_cpu_time=[]
    for item in cpu:
        tmp_cpu.append(item['cpu_value'])
        tmp_cpu_time.append(item["time"])

    for item in mem:
        tmp_mem.append(item['mem_value'])

    line=pyecharts.Line(u"CPU/内存百分比图片",background_color="#FFFFFF")
    line.add("CPU",tmp_cpu_time,tmp_cpu,mark_point=["average","max","min"],line_color="red")
    line.add("MEM", tmp_cpu_time, tmp_mem,mark_point=["average","max","min"],line_color="blue",label_text_color="blue")
    line.render(path)
    pass

def make_mem_picture(used_mem,ava_mem, path):
    tmp_used_mem = []
    tmp_cpu_time=[]
    tmp_ava_mem=[]
    for item in used_mem:
        tmp_used_mem.append(item['mem_value'])
        tmp_cpu_time.append(item["time"])
    for item in ava_mem:
        tmp_ava_mem.append(item['ava_mem_value'])

    kwargs = dict(
        mark_point=['average', 'max', 'min'],
        mark_point_symbol='diamond',
        mark_point_textcolor='#FFFFFF'
    )
    line=pyecharts.Line(u"内存使用情况(单位 KB)",background_color="#FFFFFF")
    line.add(u"应用使用内存", tmp_cpu_time, tmp_used_mem,line_color="red",**kwargs)
    line.add(u"系统剩余内存", tmp_cpu_time, tmp_ava_mem,
             line_color="blue", **kwargs)
    line.render(path)
    pass

def make_graph1(cpu,mem,path,logger):
    '''
    :param cpu: cpu列表
    :param mem: 内存列表
    :path 存储文件路径
    :return
    '''
    # cpu = map(lambda x: x['cpu_value'], cpu)
    # mem = map(lambda x: x['mem_value'], mem)
    tmp_cpu=[]
    tmp_mem=[]
    for item in cpu:
        tmp_cpu.append(item['cpu_value'])
    cpu1=tmp_cpu

    for item in mem:
        tmp_mem.append(item['mem_value'])
    mem1=tmp_mem


    # pl.plot(cpu,'r')
    # pl.plot(mem,'g')

    pl.title('perfomance')
    pl.xlabel('second')
    pl.ylabel('percent')

    pl.plot(cpu1,color='red',linewidth=2.5,linestyle='-',label='cpu')
    pl.plot(mem1,color='blue',linewidth=2.5,linestyle='-',label='mem')

    pl.legend(loc='upper left')
    pl.xlim(0.0,len(mem))
    pl.ylim(0.0,100.0)

    pl.savefig(path)
    pl.close()  # 必须关闭,不然值会在内存中不销毁
    logger.info('Report:%s' % path)

def f(x):
    return x['cpu_value']

if __name__ == '__main__':
    import random


    lst = []
    for i in range(10):
        lst.append({"time": "18:00:08","mem_value":random.randint(10000, 60000)})


    make_mem_picture(lst,  'D:/1.png')



